package com.neusoft.controller;

import com.neusoft.pojo.SysFile;
import com.neusoft.service.IDocumentService;
//import com.sun.org.apache.xerces.internal.dom.PSVIDocumentImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import java.util.Date;


@RestController
@RequestMapping("/file")
@CrossOrigin
public class Document {
    String Address[]={"",""};
    @Autowired(required = false)
    IDocumentService iDocumentService;

//    @RequestMapping("searchlist")
//    public java.util.List<SysFile>list(SysFile sysFile, String id){
//        return iDocumentService.sysPersonFileList(sysFile,Integer.parseInt(id));
//    }

//  查询所有公共属性（公共文档类别ID不为空）的文档（）
    @RequestMapping("listAll")
    public java.util.List<SysFile>listAll(SysFile sysFile){
        return iDocumentService.searchList(sysFile);
    }
//    根据员工id查询个人文档
    @RequestMapping("listOne")
    public java.util.List<SysFile>listOne(String workerId) {
        int workerId1=Integer.parseInt(workerId);
        System.out.println("员工ID="+workerId);
        return iDocumentService.listOne(workerId1);
    }
    //根据索引查询相关的个人文档
    @RequestMapping("searchMyFile")
    public java.util.List<SysFile>searchMyFile(String workerId, String fileType, String subjectKeywords) {
        int id1=Integer.parseInt(workerId);
        System.out.println("workerId="+workerId);
        System.out.println("fileType="+fileType);
        System.out.println("subjectKeywords="+subjectKeywords);
        return iDocumentService.searchMyFile(id1,fileType,subjectKeywords);
    }
    //删除文档
    @RequestMapping("deleteFile")
    public java.util.List<SysFile>delete(int fileId,int workerId){
        System.out.println("fileId="+fileId);
        System.out.println("workerId="+workerId);
        //先删除再查表
        //iDocumentService.deleteFile(fileId)
        return iDocumentService.listOne(workerId);
    }
    //上传文档
    @RequestMapping("insertFileId")
    public boolean insertFileId(int workerId,String workerName){
        iDocumentService.insertFileId(workerId,workerName);
        return true;
    }
    @RequestMapping("getFileId")
    public java.util.List<SysFile>getFileId(int workerId){
        return iDocumentService.getFileId(workerId);
    }
    @RequestMapping("insertAddress")
    public boolean insertAddress(int fileId,String fileAddress){
        //1System.out.println("完成前"+fileAddress);
        iDocumentService.insertAddress(fileId,fileAddress);
        System.out.println("666"+fileId);
        return true;
    }
    @RequestMapping("uploadAddress")
    public boolean insertAddress(int fileId,String fileTitle,String fileType,String subjectKeywords){
        iDocumentService.uploadAddress(fileId,fileTitle,fileType,subjectKeywords);
        return true;
    }

//    @RequestMapping("insert")
////    int workerId,String fileTitle,String fileType,String subjectKeywords,String fileAddress
//    public boolean insert(String fileAddress){
//        System.out.println(workerId+fileTitle+ fileType+ subjectKeywords+ fileAddress);
//        iDocumentService.insertAddress
////        iDocumentService.insertFile( workerId,fileTitle, fileType, subjectKeywords, fileAddress);
//        return true;
//    }








    @RequestMapping("upload")
    public String[] uploadFile(MultipartFile file, HttpServletRequest req){
        if(!file.isEmpty()){
            //获得服务器地址
            String uploadPath = req.getServletContext().getRealPath("/upfiles/");
            //System.out.println("上传地址:"+uploadPath);

            File uploadDir = new File(uploadPath);
            if (!uploadDir.exists()) {
                uploadDir.mkdir(); // 如果目录不存在则创建
            }
            String OriginalFilename = file.getOriginalFilename();//获取原文件名    ab.c.txt
            String suffixName = OriginalFilename.substring(OriginalFilename.lastIndexOf("."));//获取原文件后缀名 txt

            //重新随机生成名字
            String filename = UUID.randomUUID().toString() +suffixName;
            File serverFile = new File(uploadPath+"\\"+filename);  //根据服务器地址和新的文件名创建的File对象
            try {
                file.transferTo(serverFile); //把上传的文件保存至本地
                /**
                 * 这里应该把filename保存到数据库,供前端访问时使用
                 */
                //System.out.println(filename+">>>>>>>>>>>.");
                String suffixName1=suffixName.substring(1,suffixName.length());
                Address[1]=suffixName1;
                Address[0]="http://localhost:8080/upfiles/"+filename;//上传成功，返回保存的文件地址
                return Address;

            }catch (IOException e){
                e.printStackTrace();
                System.out.println("上传失败");
                return Address;
            }
        }else{
            System.out.println("文件为空");
            return Address;
        }
    }
}
